﻿using LoveNote.Application.Share;
using LoveNote.Application.Share.Login;
using LoveNote.Application.Share.Message;
using LoveNote.Application.Share.Shell;
using LoveNote.Shell.Services;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Input;
using TookKit.Mvvm.Attributes;
using TookKit.Mvvm.Bindings;
using TookKit.Mvvm.Enums;
using TookKit.Mvvm.Events;

namespace LoveNote.Shell.ViewModels
{
    [DependencyInjection(Lifetime.Singleton)]
    internal class ShellViewModel : ObservableObject
    {
        private ContentControl container = null;
        public ICommand WindowLoadedCommand { get; }
        public ICommand ContentControlLoadedCommand { get; }
        public ISession Session { get; }   
        public IShellManager ShellManager { get; }

        public ShellViewModel(ISession session, IShellManager shellManager)
        {
            Session = session;
            ShellManager = shellManager;
            WindowLoadedCommand = new RelayCommand<Window>(OnWindowLoadedCommand);
            ContentControlLoadedCommand = new RelayCommand<ContentControl>(OnContentControlLoadedCommand);

            //订阅登录成功事件
            EventBus.Default.Subscribe<LoginMessage>((sender, message) =>
            {
                container.Content = TookKit.Mvvm.Container.Default.GetService(ViewNames.MainView);//切换到主界面

            });

            //切换用户事件
            EventBus.Default.Subscribe<LogoutMessage>((sender, message) =>
            {
                container.Content = TookKit.Mvvm.Container.Default.GetService(ViewNames.LoginView);//切换到登录界面
            });
        }

        private void OnContentControlLoadedCommand(ContentControl container)
        {
            this.container = container;
            container.Content = TookKit.Mvvm.Container.Default.GetService(ViewNames.LoginView);//首先启动登录界面
        }

        private void OnWindowLoadedCommand(Window window)
        {
            if (Session.MainWindow == null)
            {
                Session.MainWindow = window;
            }
        }
    }
}
